package com.android.meige.model.group;

import android.content.Context;
import android.content.Intent;
import android.graphics.Paint;
import android.support.v4.app.FragmentActivity;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.AbsoluteSizeSpan;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.meige.R;
import com.android.meige.activity.BaseObject;
import com.android.meige.utils.ConstantsUrl;
import com.android.meige.utils.MyLog;
import com.android.meige.utils.ObjectUtils;
import com.chad.library.adapter.base.BaseMultiItemQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.mcxtzhang.nestlistview.NestFullListView;
import com.mcxtzhang.nestlistview.NestFullListViewAdapter;
import com.mcxtzhang.nestlistview.NestFullViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2017/6/27.
 */

public class MainlistBidCompetitionAdapt extends BaseMultiItemQuickAdapter<BidCompetitionAGoodsListData.DataBean.ListBean, BaseViewHolder> {
    private String type;
    private ArrayList<MainThematicActivity_Insert.DataBean.ListBean> ta_insert;
    private FragmentActivity mcontext;
    private int limitposition;
    private int datasize;
    public MainlistBidCompetitionAdapt(FragmentActivity context, ArrayList<MainThematicActivity_Insert.DataBean.ListBean> ta_insert, List<BidCompetitionAGoodsListData.DataBean.ListBean> data, String type) {
        super(data);
        this.mcontext = context;
        this.type = type;
        this.ta_insert = ta_insert;
        addItemType(MainList.MainListdata.Mainlistdata.ONE, R.layout.item_list_main);
       // addItemType(MainList.MainListdata.Mainlistdata.DOUBLE, R.layout.item_grid_detail_like);
    }
    public MainlistBidCompetitionAdapt(Context context, List<BidCompetitionAGoodsListData.DataBean.ListBean> data, String type) {
        super(data);
        this.type = type;
        addItemType(MainList.MainListdata.Mainlistdata.ONE, R.layout.item_list_main);
    }
    public ArrayList<MainThematicActivity_Insert.DataBean.ListBean> getTa_insert() {
        return ta_insert;
    }

    public void setTa_insert(ArrayList<MainThematicActivity_Insert.DataBean.ListBean> ta_insert) {
        this.ta_insert = ta_insert;
        limitposition = 4*ta_insert.size();
    }

    @Override
    protected void convert(BaseViewHolder holder, BidCompetitionAGoodsListData.DataBean.ListBean mainlistdata) {
        switch (holder.getItemViewType()) {
            case MainList.MainListdata.Mainlistdata.ONE:
                ((TextView) holder.getView(R.id.item_list_sort_aa_before)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG);
                ObjectUtils.photo2(mContext, mainlistdata.getThumb(), ((ImageView) holder.getView(R.id.main_list_item_image)));
                holder.setText(R.id.main_list_item_tuannumber, mainlistdata.getTeam_num() + "人团");
                holder.setText(R.id.ll_start_soon_num, mainlistdata.getTeam_num() + "人团");
                holder.setText(R.id.ll_free_num, mainlistdata.getTeam_num() + "人团");
                holder.setText(R.id.main_list_item_alltuannumber2, "已团" + mainlistdata.getTeam_num() + "件");
                String price1 = mainlistdata.getTeam_price();
                SpannableString msp = new SpannableString("￥" + price1);
                msp.setSpan(new AbsoluteSizeSpan(13, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                holder.setText(R.id.main_list_item_money_after, msp);
                int endtime = Integer.parseInt(mainlistdata.getC_time());
                        holder.setText(R.id.title_type, "首页竞价");
                        holder.setText(R.id.main_list_item_title, "            " + mainlistdata.getName());
                /**
                 * 插入专题活动
                 */
                final int taposition=holder.getAdapterPosition();
                MyLog.e("charuposition",taposition+"");
                MyLog.e("limitposition",limitposition+"");
                if(mcontext instanceof FragmentActivity) {
                    if (taposition % 4 == 0 && taposition <= limitposition) {

                        holder.getView(R.id.ll_tainsert).setVisibility(View.VISIBLE);
                        holder.getView(R.id.progress_hot).setVisibility(View.INVISIBLE);
                        TextView  more_tv = (TextView)holder.getView(R.id.more_tv);
                        //水平滚动列表
                        NestFullListView tabrow = (NestFullListView) holder.getView(R.id.grid);
                        //        //动态设置选项卡宽度，超过2个可以滑动查看
                        DisplayMetrics metrics = new DisplayMetrics();
                        mcontext.getWindowManager().getDefaultDisplay().getMetrics(metrics);
                        int width = (int) (metrics.widthPixels / 2.5);
                        final LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, LinearLayout.LayoutParams.MATCH_PARENT);
                        MyLog.e("listid", ta_insert.get(taposition / 4 - 1).getId() + "");
                        tabrow.setAdapter(new NestFullListViewAdapter<MainThematicActivity_Insert.DataBean.ListBean.ApplyBean>(R.layout.list_main_hor_adapter,ta_insert.get(taposition / 4 - 1).getApply_list()) {
                            @Override
                            public void onBind(int position, MainThematicActivity_Insert.DataBean.ListBean.ApplyBean testBean, NestFullViewHolder holder) {
                                (holder.getView(R.id.item_grid_detail_like_ll)).setLayoutParams(params);
                                ((TextView) holder.getView(R.id.item_grid_detail_like_number_before)).getPaint().setFlags(Paint.STRIKE_THRU_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);
                                String price1 = testBean.getTeam_price();
                                SpannableString msp = new SpannableString("￥" + price1);
                                msp.setSpan(new AbsoluteSizeSpan(10, true), 0, 1, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                                ((TextView) holder.getView(R.id.item_grid_detail_like_number_after)).setText(msp);
                                holder.setText(R.id.item_grid_detail_like_number_before, "￥" + testBean.getPrice());
                                holder.setText(R.id.item_grid_detail_like_title, testBean.getName());
                                ObjectUtils.photo2(mcontext, testBean.getImg_cover(), ((ImageView) holder.getView(R.id.item_grid_detail_like_img)));

                            }
                        });

                        tabrow.setOnItemClickListener(new NestFullListView.OnItemClickListener() {
                            @Override
                            public void onItemClick(NestFullListView nestFullListView, View view, int pos) {
                                // ObjectUtils.ToDetailActivity(mcontext, 1, ta_insert.get(taposition / 4 - 1).getApply_list().get(pos).getSid(), "");
                            }
                        });
                        more_tv.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(mcontext, BaseObject.class);
                                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_NEED_ID, ta_insert.get(taposition / 4 - 1).getId());
                                intent.putExtra(ConstantsUrl.INTENT_KEY.MAIN_GROUP_OBJEXT_SORT_TITLE,"专题活动");
                                mcontext.startActivity(intent);
                            }
                        });

                    }
                    else
                    {
                        holder.getView(R.id.ll_tainsert).setVisibility(View.GONE);
                    }
                }

                holder.addOnClickListener(R.id.main_list_item_go_kai)
                        .addOnClickListener(R.id.ll_free)
                        .addOnClickListener(R.id.ll_start_soon)
                        .addOnClickListener(R.id.ll_result);


                break;
        }

    }


}
